package com.situ.hospital.servlet;

import com.situ.hospital.pojo.Category;
import com.situ.hospital.pojo.Medicine;
import com.situ.hospital.pojo.query.MedicineQuery;
import com.situ.hospital.pojo.vo.InventoryVO;
import com.situ.hospital.pojo.vo.MedicineVO;
import com.situ.hospital.service.ICategoryService;
import com.situ.hospital.service.IMedicineService;
import com.situ.hospital.service.impl.CategoryServiceImpl;
import com.situ.hospital.service.impl.MedicineServiceImpl;
import com.situ.hospital.util.JSONUtil;
import com.situ.hospital.util.MyBeanUtil;
import com.situ.hospital.util.PageResult;
import com.situ.hospital.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/medicine")
public class MedicineServlet extends HttpServlet {

    private IMedicineService medicineService = new MedicineServiceImpl();
    private ICategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "exportExcel":
                exportExcel(req,resp);
                break;
            case "importExcel":
                importExcel(req,resp);
                break;
            case "selectAll":
                selectAll(req,resp);
                break;
            case "selectQuantity":
                selectQuantity(req,resp);
                break;
            case "selectMap":
                selectMap(req,resp);
                break;
        }
    }

    private void selectMap(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.selectMap");
        String id = req.getParameter("id");
        Medicine medicine = medicineService.selectById(Integer.parseInt(id));
        List<Category> list = categoryService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("medicine",medicine);
        map.put("category",list);
        JSONUtil.toJSON(resp, Result.ok(map));
    }

    private void selectQuantity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.selectQuantity");
        String id = req.getParameter("medicineId");
        System.out.println(id);
        List<InventoryVO> list = medicineService.selectQuantity(Integer.parseInt(id));
        JSONUtil.toJSON(resp, list);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.selectAll");
        List<Medicine> list = medicineService.selectAll();
        JSONUtil.toJSON(resp, list);
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.importExcel");
        medicineService.importExcel(req);
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.exportExcel");
        medicineService.exportExcel(resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.update");
        Medicine medicine = MyBeanUtil.copyToBean(req, Medicine.class);
        medicineService.update(medicine);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.add");
        Medicine medicine = MyBeanUtil.copyToBean(req, Medicine.class);
        medicineService.add(medicine);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        medicineService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.deleteById");
        String id = req.getParameter("id");
        medicineService.deleteById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("MedicineServlet.selectByPage");
        MedicineQuery medicineQuery = MyBeanUtil.copyToBean(req, MedicineQuery.class);
        PageResult<MedicineVO> pageResult = medicineService.selectByPage(medicineQuery);
        JSONUtil.toJSON(resp, pageResult);
    }
}
